Page({
  data: {
    url_temp: 'null',
    mode:'',
    devicePosition:'',
    flash:'',
    frameSize:'',
    quality:'', 
    frameData:'',
    jingtou: 'front'
  },

  error(e) {
    console.log(e.detail);
    my.alert({content : "cameraError" + JSON.stringify(e)});
  },
  

  scanCode(e){
    console.log(e.detail);
    my.alert({content : "cameraScanCode" + JSON.stringify(e)});
  },

  stop(e) {
    console.log(e.detail);
    my.alert({content : "cameraStop" + JSON.stringify(e)});
  },


  off(e) {
    console.log(e.detail);
    my.alert({content : "cameraOff" + JSON.stringify(e)});
  },


  setFlash(){
    if( this.data.flash== '' || this.data.flash== 'off'){
      this.setData({
        flash: 'auto'
      })
      my.showToast({ content: "闪光灯已设置自动"});
    } else if(this.data.flash == 'auto'){
      this.setData({
        flash: 'on'
      })
      my.showToast({ content: "已打开闪光灯"});
    } else if(this.data.flash == 'on') {
      this.setData({
        flash: 'off'
      })
      my.showToast({ content: "已关闭闪光灯"});
    }
  },
  
  setFrameSize(){

    if( this.data.frameSize== '' || this.data.frameSize== 'large'){
      this.setData({
        frameSize: 'small'
      })
      my.showToast({ content: "已切换到small模式"});
    } else if(this.data.frameSize == 'small'){
      this.setData({
        frameSize: 'medium'
      })
      my.showToast({ content: "已切换到medium模式"});
    } else if(this.data.frameSize == 'medium') {
      this.setData({
        frameSize: 'large'
      })
      my.showToast({ content: "已切换到large模式"});
    }
  },
  
  setdevicePosition(){
    
    if( this.data.devicePosition != 'back'){
      this.setData({
        devicePosition: 'back'
      })
      my.showToast({ content: "已切换到后置摄像头"});
    } else {
      this.setData({
        devicePosition: 'front'
      })
      my.showToast({ content: "已切换到前置摄像头"});
    }
    
  },

  setMode(){
    if(this.data.mode != 'normal'){
      this.setData({
        mode: 'normal'
      })
      my.showToast({ content: "已切换到相机模式"});
    } else {
      this.setData({
        mode: 'scanCode'
      })
      my.showToast({ content: "已切换到扫码模式"});
    }  
  },
  onLoad() {
    this.camera_take = my.createCameraContext('camera_take');
  },

  setQuality(){

    if( this.data.quality== '' || this.data.quality== 'high'){
      this.setData({
        quality: 'low'
      })
      my.showToast({ content: "已切换到低质量拍照"});
    } else if(this.data.quality == 'low'){
      this.setData({
        quality: 'normal'
      })
      my.showToast({ content: "已切换到普通质量拍照"});
    } else if(this.data.quality == 'normal') {
      this.setData({
        quality: 'high'
      })
      my.showToast({ content: "已切换到高质量拍照"});
    }
  },
  clear(){
    this.setData({
      photoUrl:''
    })
  },

  takePhoto(){
    this.camera_take.takePhoto({
        quality:this.data.quality,
        success: (res)=>{
          this.setData({
            url_temp: res.tempImagePath,
            photoUrl: res.tempImagePath
          })
          my.showToast({ content: "拍照成功!"+JSON.stringify(res)});


        },
        fail: (err) => {
          my.alert({
            content: 'takePhoto error' + JSON.stringify(err) 
          });
        }
      })
  },

  startCameraFrameListener(){
    if(this.cameraFrameListener==null){
      this.cameraFrameListener = this.camera_take.onCameraFrame((frame) => {
        this.setData({
          frameData: frame.width + "*"+frame.height
        })
      })
    }
    this.cameraFrameListener.start()
  },

  stopCameraFrameListener(){
    if(this.cameraFrameListener==null){
      my.showToast({ content: 'cameraFrameListener is null'});
    } else {
      this.cameraFrameListener.stop()
    }
  }

});
